<?php 
class ControllerCatalogProduct extends Controller {
	private $error = array(); 
     
  	public function index() {
		$this->load->language('catalog/product');
    	
		$this->document->title = $this->language->get('heading_title'); 
		
		$this->load->model('catalog/product');
		
		$this->getList();
  	}
  
  	public function insert() {
    	$this->load->language('catalog/product');

    	$this->document->title = $this->language->get('heading_title'); 
		
		$this->load->model('catalog/product');
		
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_catalog_product->addProduct($this->request->post);
	  		
			$this->session->data['success'] = $this->language->get('text_success');
	  
			$url = $this->getUrlParams();   			             
			$this->redirect(HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . $url);
    	}
	
    	$this->getForm();
  	}

  	public function update() {
    	$this->load->language('catalog/product');

    	$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('catalog/product');
	
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {    		
			$this->model_catalog_product->editProduct($this->request->get['product_id'], $this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = $this->getUrlParams();
			$this->redirect(HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . $url);
		}

    	$this->getForm();
  	}

  	public function delete() {
    	$this->load->language('catalog/product');

    	$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('catalog/product');
		
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $product_id) {
				$this->model_catalog_product->deleteProduct($product_id);
	  		}

			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = $this->getUrlParams(); 
			$this->redirect(HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . $url);
		}

    	$this->getList();
  	}

  	public function copy() {
    	$this->load->language('catalog/product');

    	$this->document->title = $this->language->get('heading_title');
		
		$this->load->model('catalog/product');
		
		if (isset($this->request->post['selected']) && $this->validateCopy()) {
			foreach ($this->request->post['selected'] as $product_id) {
				$this->model_catalog_product->copyProduct($product_id);
	  		}

			$this->session->data['success'] = $this->language->get('text_success');
			
			$url = $this->getUrlParams();
			$this->redirect(HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . $url);
		}

    	$this->getList();
  	}
	
  	private function getList() {				
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'pd.name';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}

		if (isset($this->request->get['filter_name'])) {
			$filter_name = $this->request->get['filter_name'];
		} else {
			$filter_name = NULL;
		}

		if (isset($this->request->get['filter_model'])) {
			$filter_model = $this->request->get['filter_model'];
		} else {
			$filter_model = NULL;
		}

		if (isset($this->request->get['filter_quantity'])) {
			$filter_quantity = $this->request->get['filter_quantity'];
		} else {
			$filter_quantity = NULL;
		}

//filter category start//
		if (isset($this->request->get['filter_category'])) {
			$filter_category = $this->request->get['filter_category'];
		} else {
			$filter_category = NULL;
		}
//filter category end//

        if (isset($this->request->get['filter_sku'])) {
            $filter_sku = $this->request->get['filter_sku'];
        } else {
            $filter_sku = NULL;
        }

        if (isset($this->request->get['filter_manufacturer'])) {
            $filter_manufacturer = $this->request->get['filter_manufacturer'];
        } else {
            $filter_manufacturer = NULL;
        }
        
		if (isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
		} else {
			$filter_status = NULL;
		}
		
        if (isset($this->request->get['filter_price'])) {
            $filter_price = $this->request->get['filter_price'];
        } else {
            $filter_price = NULL;
        }
        
		$url = $this->getUrlParams();		

  		$this->document->breadcrumbs = array();
   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);                                     
   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . $url,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
		
		$this->data['insert'] = HTTPS_SERVER . 'index.php?route=catalog/product/insert&token=' . $this->session->data['token'] . $url;
		$this->data['copy'] = HTTPS_SERVER . 'index.php?route=catalog/product/copy&token=' . $this->session->data['token'] . $url;	
		$this->data['delete'] = HTTPS_SERVER . 'index.php?route=catalog/product/delete&token=' . $this->session->data['token'] . $url;
    	
		$this->data['products'] = array();

		$data = array(
			'filter_name'	        => $filter_name, 
			'filter_model'	        => $filter_model,
			'filter_quantity'       => $filter_quantity,
            'filter_category'       => $filter_category,
            'filter_sku'            => $filter_sku,             
			'filter_status'         => $filter_status,
			'sort'                  => $sort,
			'order'                 => $order,
			'start'                 => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'                 => $this->config->get('config_admin_limit'),
            'filter_manufacturer'   => $filter_manufacturer,
            'filter_price'          => $filter_price,
		);
		
		$this->load->model('tool/image');
		
		$product_total = $this->model_catalog_product->getTotalProducts($data);
			
		$results = $this->model_catalog_product->getProducts($data);

//filter category start//
		$this->load->model('catalog/category');
		//$this->data['categories'] = $this->model_catalog_category->getCategories(0);
        $this->data['categories'] = $this->model_catalog_category->getEnableCategories(0);
//filter category end//

        $this->load->model('catalog/manufacturer');        
        $this->data['manufacturers'] = $this->model_catalog_manufacturer->getManufacturers();
        
		foreach ($results as $result) {
			$action = array();
			
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => HTTPS_SERVER . 'index.php?route=catalog/product/update&token=' . $this->session->data['token'] . '&product_id=' . $result['product_id'] . $url
			);

//filter category start//
            $category =  $this->model_catalog_product->getProductCategories($result['product_id']);
//filter category end//

            $manufacturer = $this->model_catalog_manufacturer->getManufacturer($result['manufacturer_id']);
            
			if ($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
				$image = $this->model_tool_image->resize($result['image'], 40, 40);
			} else {
				$image = $this->model_tool_image->resize('no_image.jpg', 40, 40);
			}
            
            $this->load->model('catalog/product_sort');
            $product_sort_order = $this->model_catalog_product_sort->getProductSortOrderByCategory($result['product_id'], $category);            
			$sort_orders = array();
            foreach($product_sort_order as $sort_item){
                $sort_orders[] = $sort_item['category_id'];     
            }
            $sort_orders = count($sort_orders) < count($category) ? $category : $sort_orders;
            
      		$this->data['products'][] = array(
				'product_id'    => $result['product_id'],
				'name'          => $result['name'],
				'model'         => $result['model'],
//filter category start//
				'category'      => $category,
//filter category end//
                'sku'           => $result['sku'],                
				'image'         => $image,
				'quantity'      => $result['quantity'],
				'status'        => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'selected'      => isset($this->request->post['selected']) && in_array($result['product_id'], $this->request->post['selected']),
				'action'        => $action,
                //'manufacturer' => $manufacturer, 
                'manufacturer'  => isset($manufacturer['name']) ? $manufacturer['name'] : '', 
                'price'         => $result['price'],
                'sort_order'    => $product_sort_order,
                'ref_sort'      => $sort_orders,
			);
    	}
		
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');
        $this->data['text_video_manager'] = $this->language->get('text_video_manager');

		$this->data['column_image'] = $this->language->get('column_image');
		$this->data['column_name'] = $this->language->get('column_name');
        $this->data['column_model'] = $this->language->get('column_model');
//filter category start//
        $this->data['column_category'] = $this->language->get('column_category');
//filter category end//
        $this->data['column_sku'] = $this->language->get('column_sku');
		$this->data['column_quantity'] = $this->language->get('column_quantity');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_action'] = $this->language->get('column_action');
		
		$this->data['button_copy'] = $this->language->get('button_copy');
		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');
		$this->data['button_filter'] = $this->language->get('button_filter');
 
 		$this->data['token'] = $this->session->data['token'];
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = $this->getFilter();
        
		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} 
        else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
					
		$this->data['sort_name'] = HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . '&sort=pd.name' . $url;
		$this->data['sort_model'] = HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . '&sort=p.model' . $url;
		$this->data['sort_quantity'] = HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . '&sort=p.quantity' . $url;
//filter category start//
		$this->data['sort_category'] = HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . '&sort=category' . $url;
//filter category end//
        $this->data['sort_sku'] = HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . '&sort=p.sku' . $url;
        $this->data['sort_status'] = HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . '&sort=p.status' . $url;
		$this->data['sort_order'] = HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . '&sort=p.sort_order' . $url;
        $this->data['sort_manufacturer'] = HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . '&sort=manufacturer' . $url;
        $this->data['sort_price'] = HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . '&sort=p.price' . $url;        
        $this->data['sort_sort_order'] = HTTPS_SERVER . 'index.php?route=catalog/product&sort=pcs.product_sort_order' . $url . '&token=' . $this->session->data['token'];        
		
		$url = $this->getFilter();

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		$pagination = new Pagination();
		$pagination->total = $product_total; 
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . $url . '&page={page}';
			
		$this->data['pagination'] = $pagination->render();
	
		$this->data['filter_name'] = $filter_name;
		$this->data['filter_model'] = $filter_model;
//filter category start//
		$this->data['filter_category'] = $filter_category;
//filter category end//
        $this->data['filter_sku'] = $filter_sku;
        $this->data['filter_manufacturer'] = $filter_manufacturer;  
		$this->data['filter_quantity'] = $filter_quantity;
		$this->data['filter_status'] = $filter_status;
        $this->data['filter_price'] = $filter_price;
		
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'catalog/product_list.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
  	}

  	private function getForm() {
        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_enabled'] = $this->language->get('text_enabled');
        $this->data['text_disabled'] = $this->language->get('text_disabled');
        $this->data['text_none'] = $this->language->get('text_none');
        $this->data['text_yes'] = $this->language->get('text_yes');
        $this->data['text_no'] = $this->language->get('text_no');
		$this->data['text_plus'] = $this->language->get('text_plus');
		$this->data['text_minus'] = $this->language->get('text_minus');
		$this->data['text_default'] = $this->language->get('text_default');
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');
        $this->data['text_video_manager'] = $this->language->get('text_video_manager');
		$this->data['text_option'] = $this->language->get('text_option');
		$this->data['text_option_value'] = $this->language->get('text_option_value');
		$this->data['text_select'] = $this->language->get('text_select');
		$this->data['text_none'] = $this->language->get('text_none');
		
		$this->data['tab_shipping'] = $this->language->get('tab_shipping');
		$this->data['tab_links'] = $this->language->get('tab_links');
		
		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_meta_keywords'] = $this->language->get('entry_meta_keywords');
		$this->data['entry_meta_description'] = $this->language->get('entry_meta_description');
		$this->data['entry_description'] = $this->language->get('entry_description');
		$this->data['entry_store'] = $this->language->get('entry_store');
		$this->data['entry_keyword'] = $this->language->get('entry_keyword');
    	$this->data['entry_model'] = $this->language->get('entry_model');
		$this->data['entry_sku'] = $this->language->get('entry_sku');
        $this->data['entry_product_number'] = $this->language->get('entry_product_number');
		$this->data['entry_location'] = $this->language->get('entry_location');
		$this->data['entry_minimum'] = $this->language->get('entry_minimum');
		$this->data['entry_manufacturer'] = $this->language->get('entry_manufacturer');
    	$this->data['entry_shipping'] = $this->language->get('entry_shipping');
    	$this->data['entry_date_available'] = $this->language->get('entry_date_available');
    	$this->data['entry_quantity'] = $this->language->get('entry_quantity');
		$this->data['entry_stock_status'] = $this->language->get('entry_stock_status');
    	$this->data['entry_status'] = $this->language->get('entry_status');
    	$this->data['entry_tax_class'] = $this->language->get('entry_tax_class');
    	$this->data['entry_price'] = $this->language->get('entry_price');
		$this->data['entry_cost'] = $this->language->get('entry_cost');
		$this->data['entry_subtract'] = $this->language->get('entry_subtract');
    	$this->data['entry_weight_class'] = $this->language->get('entry_weight_class');
    	$this->data['entry_weight'] = $this->language->get('entry_weight');
		$this->data['entry_dimension'] = $this->language->get('entry_dimension');
		$this->data['entry_length'] = $this->language->get('entry_length');
    	$this->data['entry_image'] = $this->language->get('entry_image');
        $this->data['entry_video'] = $this->language->get('entry_video');
        $this->data['entry_video_upload'] = $this->language->get('entry_video_upload');
    	$this->data['entry_download'] = $this->language->get('entry_download');
    	$this->data['entry_category'] = $this->language->get('entry_category');
		$this->data['entry_related'] = $this->language->get('entry_related');
		$this->data['entry_option'] = $this->language->get('entry_option');
		$this->data['entry_option_value'] = $this->language->get('entry_option_value');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		$this->data['entry_prefix'] = $this->language->get('entry_prefix');
		$this->data['entry_customer_group'] = $this->language->get('entry_customer_group');
		$this->data['entry_date_start'] = $this->language->get('entry_date_start');
		$this->data['entry_date_end'] = $this->language->get('entry_date_end');
		$this->data['entry_priority'] = $this->language->get('entry_priority');
		$this->data['entry_tags'] = $this->language->get('entry_tags');
        $this->data['entry_color'] = $this->language->get('entry_color');
        $this->data['entry_color_name'] = $this->language->get('entry_color_name');
        $this->data['entry_size_guide'] = $this->language->get('entry_size_guide');
        $this->data['entry_product_option_number'] = $this->language->get('entry_product_option_number');
        $this->data['entry_low_stock_warning'] = $this->language->get('entry_low_stock_warning');
        $this->data['entry_delivery_time_instock'] = $this->language->get('entry_delivery_time_instock');
        $this->data['entry_delivery_time_outstock'] = $this->language->get('entry_delivery_time_outstock');
        $this->data['entry_quantity'] = $this->language->get('entry_quantity'); 
		
    	$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_add_option'] = $this->language->get('button_add_option');
		$this->data['button_add_option_value'] = $this->language->get('button_add_option_value');
		$this->data['button_add_discount'] = $this->language->get('button_add_discount');
		$this->data['button_add_special'] = $this->language->get('button_add_special');
		$this->data['button_add_image'] = $this->language->get('button_add_image');
		$this->data['button_remove'] = $this->language->get('button_remove');
        $this->data['button_add_color'] = $this->language->get('button_add_color');
        $this->data['button_update_color'] = $this->language->get('button_update_color');
        $this->data['button_add_attribute'] = $this->language->get('button_add_attribute');
        $this->data['button_update_attribute'] = $this->language->get('button_update_attribute');
        $this->data['button_remove_attribute'] = $this->language->get('button_remove_attribute');
        $this->data['button_remove_attribute_value'] = $this->language->get('button_remove_attribute_value');
		
    	$this->data['tab_general'] = $this->language->get('tab_general');
    	$this->data['tab_data'] = $this->language->get('tab_data');
		$this->data['tab_discount'] = $this->language->get('tab_discount');
		$this->data['tab_special'] = $this->language->get('tab_special');
		$this->data['tab_option'] = $this->language->get('tab_option');
    	$this->data['tab_image'] = $this->language->get('tab_image');
        $this->data['tab_color'] = $this->language->get('tab_color');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

 		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = '';
		}

 		if (isset($this->error['meta_description'])) {
			$this->data['error_meta_description'] = $this->error['meta_description'];
		} else {
			$this->data['error_meta_description'] = '';
		}		
   
   		if (isset($this->error['description'])) {
			$this->data['error_description'] = $this->error['description'];
		} else {
			$this->data['error_description'] = '';
		}	
		
		/*
   		if (isset($this->error['model'])) {
			$this->data['error_model'] = $this->error['model'];
		} else {
			$this->data['error_model'] = '';
		}	
		*/
		if (isset($this->error['sku'])) {
			$this->data['error_sku'] = $this->error['sku'];
		} else {
			$this->data['error_sku'] = '';
		}			
     	
		if (isset($this->error['date_available'])) {
			$this->data['error_date_available'] = $this->error['date_available'];
		} else {
			$this->data['error_date_available'] = '';
		}	
        
		$url = $this->getUrlParams();
        
  		$this->document->breadcrumbs = array();  
   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=common/home&token=' . $this->session->data['token'],
       		'text'      => $this->language->get('text_home'),
			'separator' => FALSE
   		);                                
   		$this->document->breadcrumbs[] = array(
       		'href'      => HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . $url,
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);
									
		if (!isset($this->request->get['product_id'])) {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=catalog/product/insert&token=' . $this->session->data['token'] . $url;
		} else {
			$this->data['action'] = HTTPS_SERVER . 'index.php?route=catalog/product/update&token=' . $this->session->data['token'] . '&product_id=' . $this->request->get['product_id'] . $url;
		}
		
		$this->data['cancel'] = HTTPS_SERVER . 'index.php?route=catalog/product&token=' . $this->session->data['token'] . $url;

		$this->data['token'] = $this->session->data['token'];

		if (isset($this->request->get['product_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$product_info = $this->model_catalog_product->getProduct($this->request->get['product_id']);
    	}

		$this->load->model('localisation/language');
		
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
		
		if (isset($this->request->post['product_description'])) {
			$this->data['product_description'] = $this->request->post['product_description'];
		} elseif (isset($product_info)) {
			$this->data['product_description'] = $this->model_catalog_product->getProductDescriptions($this->request->get['product_id']);
		} else {
			$this->data['product_description'] = array();
		}
		
		if (isset($this->request->post['model'])) {
      		$this->data['model'] = $this->request->post['model'];
    	} elseif (isset($product_info)) {
			$this->data['model'] = $product_info['model'];
		} else {
      		$this->data['model'] = '';
    	}

		if (isset($this->request->post['sku'])) {
      		$this->data['sku'] = $this->request->post['sku'];
    	} elseif (isset($product_info)) {
			$this->data['sku'] = $product_info['sku'];
		} else {
      		$this->data['sku'] = '';
    	}
		
        if (isset($this->request->post['product_number'])) {
              $this->data['product_number'] = $this->request->post['product_number'];
        } elseif (isset($product_info)) {
            $this->data['product_number'] = $product_info['product_number'];
        } else {
              $this->data['product_number'] = '';
        }
        
		if (isset($this->request->post['location'])) {
      		$this->data['location'] = $this->request->post['location'];
    	} elseif (isset($product_info)) {
			$this->data['location'] = $product_info['location'];
		} else {
      		$this->data['location'] = '';
    	}

		$this->load->model('setting/store');
		
		$this->data['stores'] = $this->model_setting_store->getStores();
		
		if (isset($this->request->post['product_store'])) {
			$this->data['product_store'] = $this->request->post['product_store'];
		} elseif (isset($product_info)) {
			$this->data['product_store'] = $this->model_catalog_product->getProductStores($this->request->get['product_id']);
		} else {
			$this->data['product_store'] = array(0);
		}	
		
		if (isset($this->request->post['keyword'])) {
			$this->data['keyword'] = $this->request->post['keyword'];
		} elseif (isset($product_info)) {
			$this->data['keyword'] = $product_info['keyword'];
		} else {
			$this->data['keyword'] = '';
		}
		
		if (isset($this->request->post['product_tags'])) {
			$this->data['product_tags'] = $this->request->post['product_tags'];
		} elseif (isset($product_info)) {
			$this->data['product_tags'] = $this->model_catalog_product->getProductTags($this->request->get['product_id']);
		} else {
			$this->data['product_tags'] = array();
		}
		
		if (isset($this->request->post['image'])) {
			$this->data['image'] = $this->request->post['image'];
		} elseif (isset($product_info)) {
			$this->data['image'] = $product_info['image'];
		} else {
			$this->data['image'] = '';
		}
		
		$this->load->model('tool/image');
		
		if (isset($product_info) && $product_info['image'] && file_exists(DIR_IMAGE . $product_info['image'])) {
			$this->data['preview'] = $this->model_tool_image->resize($product_info['image'], 100, 100);
		} else {
			$this->data['preview'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		}
	
        if (isset($this->request->post['video'])) {
            $this->data['video'] = $this->request->post['video'];
        } elseif (isset($product_info['video'])) {
            $this->data['video'] = $product_info['video'];
        } else {
            $this->data['video'] = '';
        }
        
		if (isset($this->request->post['youtube_video'])) {
            $this->data['youtube_video'] = $this->request->post['youtube_video'];
        } elseif (isset($product_info['youtube_video'])) {
            $this->data['youtube_video'] = $product_info['youtube_video'];
        } else {
            $this->data['youtube_video'] = '';
        }
		if (isset($this->request->post['youtube_video_width'])) {
            $this->data['youtube_video_width'] = $this->request->post['youtube_video_width'];
        } elseif (isset($product_info['youtube_video_width'])) {
            $this->data['youtube_video_width'] = $product_info['youtube_video_width'];
        } else {
            $this->data['youtube_video_width'] = '';
        }
		if (isset($this->request->post['youtube_video_height'])) {
            $this->data['youtube_video_height'] = $this->request->post['youtube_video_height'];
        } elseif (isset($product_info['youtube_video_height'])) {
            $this->data['youtube_video_height'] = $product_info['youtube_video_height'];
        } else {
            $this->data['youtube_video_height'] = '';
        }
		
		$this->load->model('catalog/manufacturer');
		
    	$this->data['manufacturers'] = $this->model_catalog_manufacturer->getManufacturers();

    	if (isset($this->request->post['manufacturer_id'])) {
      		$this->data['manufacturer_id'] = $this->request->post['manufacturer_id'];
		} elseif (isset($product_info)) {
			$this->data['manufacturer_id'] = $product_info['manufacturer_id'];
		} else {
      		$this->data['manufacturer_id'] = 0;
    	} 
		
    	if (isset($this->request->post['shipping'])) {
      		$this->data['shipping'] = $this->request->post['shipping'];
    	} elseif (isset($product_info)) {
      		$this->data['shipping'] = $product_info['shipping'];
    	} else {
			$this->data['shipping'] = 1;
		}
      	
		if (isset($this->request->post['date_available'])) {
       		$this->data['date_available'] = $this->request->post['date_available'];
		} elseif (isset($product_info)) {
			$this->data['date_available'] = date('Y-m-d', strtotime($product_info['date_available']));
		} else {
			$this->data['date_available'] = date('Y-m-d', time()-86400);
		}
											
    	if (isset($this->request->post['quantity'])) {
      		$this->data['quantity'] = $this->request->post['quantity'];
    	} elseif (isset($product_info)) {
      		$this->data['quantity'] = $product_info['quantity'];
    	} else {
			$this->data['quantity'] = 1;
		}
		
		if (isset($this->request->post['minimum'])) {
      		$this->data['minimum'] = $this->request->post['minimum'];
    	} elseif (isset($product_info)) {
      		$this->data['minimum'] = $product_info['minimum'];
    	} else {
			$this->data['minimum'] = 1;
		}
		
		if (isset($this->request->post['subtract'])) {
      		$this->data['subtract'] = $this->request->post['subtract'];
    	} elseif (isset($product_info)) {
      		$this->data['subtract'] = $product_info['subtract'];
    	} else {
			$this->data['subtract'] = 1;
		}
		
		if (isset($this->request->post['sort_order'])) {
      		$this->data['sort_order'] = $this->request->post['sort_order'];
    	} elseif (isset($product_info)) {
      		$this->data['sort_order'] = $product_info['sort_order'];
    	} else {
			$this->data['sort_order'] = 1;
		}

		$this->load->model('localisation/stock_status');
		
		$this->data['stock_statuses'] = $this->model_localisation_stock_status->getStockStatuses();
    	
		if (isset($this->request->post['stock_status_id'])) {
      		$this->data['stock_status_id'] = $this->request->post['stock_status_id'];
    	} else if (isset($product_info)) {
      		$this->data['stock_status_id'] = $product_info['stock_status_id'];
    	} else {
			$this->data['stock_status_id'] = $this->config->get('config_stock_status_id');
		}
		
    	if (isset($this->request->post['price'])) {
      		$this->data['price'] = $this->request->post['price'];
    	} else if (isset($product_info)) {
			$this->data['price'] = $product_info['price'];
		} else {
      		$this->data['price'] = '';
    	}
		
		if (isset($this->request->post['cost'])) {
      		$this->data['cost'] = $this->request->post['cost'];
    	} else if (isset($product_info)) {
			$this->data['cost'] = $product_info['cost'];
		} else {
      		$this->data['cost'] = '';
    	}

    	if (isset($this->request->post['status'])) {
      		$this->data['status'] = $this->request->post['status'];
    	} else if (isset($product_info)) {
			$this->data['status'] = $product_info['status'];
		} else {
      		$this->data['status'] = 1;
    	}
		
		$this->load->model('localisation/tax_class');
		
		$this->data['tax_classes'] = $this->model_localisation_tax_class->getTaxClasses();
    	
		if (isset($this->request->post['tax_class_id'])) {
      		$this->data['tax_class_id'] = $this->request->post['tax_class_id'];
    	} else if (isset($product_info)) {
			$this->data['tax_class_id'] = $product_info['tax_class_id'];
		} else {
      		$this->data['tax_class_id'] = 9; //0;
    	}

    	if (isset($this->request->post['weight'])) {
      		$this->data['weight'] = $this->request->post['weight'];
		} else if (isset($product_info)) {
			$this->data['weight'] = $product_info['weight'];
    	} else {
      		$this->data['weight'] = '';
    	} 
		
		$this->load->model('localisation/weight_class');
		
		$this->data['weight_classes'] = $this->model_localisation_weight_class->getWeightClasses();
    	
    	$weight_info = $this->model_localisation_weight_class->getWeightClassDescriptionByUnit($this->config->get('config_weight_class'));
    	
		if (isset($this->request->post['weight_class_id'])) {
      		$this->data['weight_class_id'] = $this->request->post['weight_class_id'];
    	} elseif (isset($product_info)) {
      		$this->data['weight_class_id'] = $product_info['weight_class_id'];
    	} elseif (isset($weight_info)) {
      		$this->data['weight_class_id'] = $weight_info['weight_class_id'];
		} else {
      		$this->data['weight_class_id'] = '';
    	}
		
		if (isset($this->request->post['length'])) {
      		$this->data['length'] = $this->request->post['length'];
    	} elseif (isset($product_info)) {
			$this->data['length'] = $product_info['length'];
		} else {
      		$this->data['length'] = '';
    	}
		
		if (isset($this->request->post['width'])) {
      		$this->data['width'] = $this->request->post['width'];
		} elseif (isset($product_info)) {	
			$this->data['width'] = $product_info['width'];
    	} else {
      		$this->data['width'] = '';
    	}
		
		if (isset($this->request->post['height'])) {
      		$this->data['height'] = $this->request->post['height'];
		} elseif (isset($product_info)) {
			$this->data['height'] = $product_info['height'];
    	} else {
      		$this->data['height'] = '';
    	}

        if (isset($this->request->post['product_cross_related'])) {
              $this->data['product_cross_related'] = $this->request->post['product_cross_related'];
        } elseif (isset($product_info)) {
            $this->data['product_cross_related'] = $product_info['cross_reference'];
        } else {
              $this->data['product_cross_related'] = 0;
        }
        
		$this->load->model('localisation/length_class');
		
		$this->data['length_classes'] = $this->model_localisation_length_class->getLengthClasses();
    	
    	$length_info = $this->model_localisation_length_class->getLengthClassDescriptionByUnit($this->config->get('config_length_class'));
    	
		if (isset($this->request->post['length_class_id'])) {
      		$this->data['length_class_id'] = $this->request->post['length_class_id'];
    	} elseif (isset($product_info)) {
      		$this->data['length_class_id'] = $product_info['length_class_id'];
    	} elseif (isset($length_info)) {
      		$this->data['length_class_id'] = $length_info['length_class_id'];
    	} else {
    		$this->data['length_class_id'] = '';
		}
		
		$this->data['language_id'] = $this->config->get('config_language_id');
		
		if (isset($this->request->post['product_option'])) {
			$this->data['product_options'] = $this->request->post['product_option'];
		} elseif (isset($product_info)) {
			$this->data['product_options'] = $this->model_catalog_product->getProductOptions($this->request->get['product_id']);
		} else {
			$this->data['product_options'] = array();
		}
		
		$this->load->model('sale/customer_group');         		
		$this->data['customer_groups'] = $this->model_sale_customer_group->getCustomerGroups();
		
		if (isset($this->request->post['product_discount'])) {
			$this->data['product_discounts'] = $this->request->post['product_discount'];
		} elseif (isset($product_info)) {
			$this->data['product_discounts'] = $this->model_catalog_product->getProductDiscounts($this->request->get['product_id']);
		} else {
			$this->data['product_discounts'] = array();
		}
        
        $this->data['default_discount_group'] = $this->config->get('config_customer_group_id');
        

		if (isset($this->request->post['product_special'])) {
			$this->data['product_specials'] = $this->request->post['product_special'];
		} elseif (isset($product_info)) {
			$this->data['product_specials'] = $this->model_catalog_product->getProductSpecials($this->request->get['product_id']);
		} else {
			$this->data['product_specials'] = array();
		}
		
        $this->load->model('setting/tooltip');        
        if (isset($this->request->post['low_stock_warning'])) {
            $this->data['low_stock_warning'] = $this->request->post['low_stock_warning'];
        } elseif (isset($product_info)) {
            $this->data['low_stock_warning'] = $product_info['low_stock_warning'];
        } else {
            $this->data['low_stock_warning'] = $this->model_setting_tooltip->getLowStockItem();
        }          
        if (isset($this->request->post['delivery_time_instock'])) {
            $this->data['delivery_time_instock'] = $this->request->post['delivery_time_instock'];
        } elseif (isset($product_info)) {
            $this->data['delivery_time_instock'] = $product_info['delivery_time_instock'];
        } else {
            $this->data['delivery_time_instock'] = $this->model_setting_tooltip->getDeliveryTimeInstock();
        }
        if (isset($this->request->post['delivery_time_outstock'])) {
            $this->data['delivery_time_outstock'] = $this->request->post['delivery_time_outstock'];
        } elseif (isset($product_info)) {
            $this->data['delivery_time_outstock'] = $product_info['delivery_time_outstock'];
        } else {
            $this->data['delivery_time_outstock'] = $this->model_setting_tooltip->getDeliveryTimeOutstock();
        }
        
        $this->load->model('catalog/attribute');                                                  
        $this->data['color_group'] = $this->model_catalog_attribute->getColorAttributes();
        $this->data['attribute_group'] = $this->model_catalog_attribute->getNormalAttributes();
              
        $attribute_groups = array();
        $p_options = $this->data['product_options'];    
        foreach($p_options as $item){             
            $p_values = $this->model_catalog_attribute->getAttributeValues($item['attribute_id']);
            foreach($p_values as &$p_value){
                //$p_value['selected'] = $this->isAttributeSelected($p_value['value_id'], $item['product_option_value']);
                $this->mergeAttributeOption($p_value, $item['product_option_value']);
            }
                            
            $attribute_groups[] = array(
                'attribute_id'      => $item['attribute_id'],
                'language'          => $item['language'],
                'description'       => $item['description'],
                'sort_order'        => $item['sort_order'],
                'product_option_id' => $item['product_option_id'], 
                'values'            => $p_values, 
            );    
        }
        $this->data['attribute_groups'] = $attribute_groups; 
        
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
        $this->data['no_color_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 20); 		
		$this->data['product_images'] = array();
		
		if (isset($product_info)) {
			$results = $this->model_catalog_product->getProductImages($this->request->get['product_id']);
			
			foreach ($results as $result) {
				if ($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
					$this->data['product_images'][] = array(
						'preview' => $this->model_tool_image->resize($result['image'], 100, 100),
						'file'    => $result['image']
					);
				} else {
					$this->data['product_images'][] = array(
						'preview' => $this->model_tool_image->resize('no_image.jpg', 100, 100),
						'file'    => $result['image']
					);
				}
			}
		}              
        
        $this->data['product_colors'] = array();
        if(isset($this->request->post['product_color'])){
            foreach ($this->request->post['product_color'] as $result) {
                if ($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
    			    $image = $result['image'];	
    			} 
                else {
                    $image = 'no_image.jpg';
    			}
                
                if ($result['color_image'] && file_exists(DIR_IMAGE . $result['color_image'])) {
    			    $color_image = $result['color_image'];	
    			} 
                else {
                    $color_image = 'no_image.jpg';
    			}
            	
    			$image_list = array();
    			if(isset($result['image_list'])){
	    			foreach ($result['image_list'] as $item) {
		    			if($item && file_exists(DIR_IMAGE . $item)) {
		    			    $image_file = $item;	
		    			} 
		                else {
		                    $image_file = 'no_image.jpg';
		    			}
		    			$image_list[] = array(
		    				'preview'       => $this->model_tool_image->resize($image_file, 100, 100),
		    				'file'          => $image_file,
		    			);
	    			}
    			}
    			$color_attribute_id = isset($result['color_attribute_id']) ? $result['color_attribute_id'] : 0;
                $this->data['product_colors'][] = array(
    				'preview'               => $this->model_tool_image->resize($image, 100, 100),
    				'file'                  => $image,
                    'name'                  => $result['name'],
                    'color_preview'         => $this->model_tool_image->resizeNoScale($color_image, 100, 20),
    				'color_image'           => $color_image,                
                	'sort_order'            => 0,
                	'image_list'	        => $image_list,
                    'color_id'              => $result['color_id'],  
                    'attribute_id'          => $color_attribute_id,  
                    'attribute_value_id'    => $result['color_av_id'],
                    'option_id'             => $result['option_id'],
                    'option_value_id'       => $result['color_ov_id'],                      
    			);	                
    		}          
        }
        elseif(isset($product_info)){
            $results = $this->model_catalog_product->getProductColors($this->request->get['product_id']);
		
    		foreach ($results as $result) {
                if ($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
    			    $image = $result['image'];	
    			} 
                else {
                    $image = 'no_image.jpg';
    			}
                
                if ($result['color_image'] && file_exists(DIR_IMAGE . $result['color_image'])) {
    			    $color_image = $result['color_image'];	
    			} 
                else {
                    $color_image = 'no_image.jpg';
    			}
                    			
    			$image_data = $this->model_catalog_product->getProductColorImages($this->request->get['product_id'], $result['product_color_id']);    		
    			$image_list = array();
    			foreach ($image_data as $item) {
    				if($item['image'] && file_exists(DIR_IMAGE . $item['image'])) {
	    			    $image_file = $item['image'];	
	    			} 
	                else {
	                    $image_file = 'no_image.jpg';
	    			}
    				$image_list[] = array(
	    				'preview'       => $this->model_tool_image->resize($image_file, 100, 100),
	    				'file'          => $image_file,
	    			);	
    			}
    			
                $this->data['product_colors'][] = array(
    				'preview'               => $this->model_tool_image->resize($image, 100, 100),
    				'file'                  => $image,
                    'name'                  => $result['color_name'],
                    'color_preview'         => $this->model_tool_image->resizeNoScale($color_image, 100, 20),
    				'color_image'           => $color_image,
                	'sort_order'            => $result['sort_order'],   
                	'image_list'	        => $image_list, 
                    'color_id'              => $result['product_color_id'],
                    'attribute_id'          => $result['attribute_id'],  
                    'attribute_value_id'    => $result['attribute_value_id'], 
                    'option_id'             => $result['option_id'],
                    'option_value_id'       => $result['option_value_id'],         
    			);                				
    		}    
        }  
        if(isset($this->request->post['color_attribute_id'])){
            $this->data['color_attribute_id'] = $this->request->post['color_attribute_id'];            
        } 
        elseif(isset($this->data['product_colors'][0]['attribute_id'])){
            $this->data['color_attribute_id'] = $this->data['product_colors'][0]['attribute_id'];
        } 
        else{
            $this->data['color_attribute_id'] = 0;
        }         
        
        if(isset($this->error['color'])){
            foreach($this->error['color'] as $key => $val){
                if(isset($this->data['product_colors'][$key])){
                    $this->data['product_colors'][$key]['error'] = $val;
                }   
            }
        }
        
		$this->load->model('catalog/download');
		
		$this->data['downloads'] = $this->model_catalog_download->getDownloads();
		
		if (isset($this->request->post['product_download'])) {
			$this->data['product_download'] = $this->request->post['product_download'];
		} elseif (isset($product_info)) {
			$this->data['product_download'] = $this->model_catalog_product->getProductDownloads($this->request->get['product_id']);
		} else {
			$this->data['product_download'] = array();
		}		
		
		$this->load->model('catalog/category');
				
		//$this->data['categories'] = $this->model_catalog_category->getCategories(0);
        $this->data['categories'] = $this->model_catalog_category->getEnableCategories(0);        
		
		if (isset($this->request->post['product_category'])) {
			$this->data['product_category'] = $this->request->post['product_category'];
		} elseif (isset($product_info)) {
			$this->data['product_category'] = $this->model_catalog_product->getProductCategories($this->request->get['product_id']);
		} else {
			$this->data['product_category'] = array();
		}		
		
 		if (isset($this->request->post['product_related'])) {
			$this->data['product_related'] = $this->request->post['product_related'];
		} elseif (isset($product_info)) {
			$this->data['product_related'] = $this->model_catalog_product->getProductRelated($this->request->get['product_id']);
		} else {
			$this->data['product_related'] = array();
		}
        
        if (isset($this->request->post['size_guide'])) {
            $this->data['size_guide'] = $this->request->post['size_guide'];
        } 
        elseif (isset($product_info)) {
            $this->data['size_guide'] = $product_info['size_guide'];
        } 
        else {
            $this->data['size_guide'] = '';
        }
				
		$this->template = 'catalog/product_form.tpl';
		$this->children = array(
			'common/header',	
			'common/footer'	
		);
		
		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
  	} 
	
    private function isAttributeSelected($attribute_value_id, $list) {
        foreach($list as $item){
            if($item['attribute_value_id'] == $attribute_value_id){
                return true;
            }    
        }
        
        return false;
    }
    
    private function mergeAttributeOption(&$attribute_value, $list) {
        foreach($list as $item){
            if($item['attribute_value_id'] == $attribute_value['value_id']){
                $attribute_value['selected']    = 1;                 
                $attribute_value['price']       = $item['price'];
                $attribute_value['quantity']    = $item['quantity'];  
                $attribute_value['subtract']    = $item['subtract'];
                $attribute_value['language']    = $item['language']; 
                $attribute_value['prefix']      = $item['prefix']; 
                $attribute_value['attribute_value_id'] = $attribute_value['value_id'];
                $attribute_value['product_option_value_id'] = $item['product_option_value_id'];
                $attribute_value['product_option_number']   = $item['product_option_number'];
                
                return true;
            }    
        }
        
        $attribute_value['selected']    = 0;
        $attribute_value['price']       = 0;
        $attribute_value['quantity']    = 0;
        $attribute_value['subtract']    = '1';
        $attribute_value['language']    = array();
        $attribute_value['prefix']      = '+';
        $attribute_value['attribute_value_id'] = $attribute_value['value_id'];
        $attribute_value['product_option_value_id'] = 0;
        $attribute_value['product_option_number']   = '';
        
        return false;
    }
    
  	private function validateForm() { 
    	if (!$this->user->hasPermission('modify', 'catalog/product')) {
      		$this->error['warning'] = $this->language->get('error_permission');
    	}

    	foreach ($this->request->post['product_description'] as $language_id => $value) {
      		if ((strlen(utf8_decode($value['name'])) < 1) || (strlen(utf8_decode($value['name'])) > 255)) {
        		$this->error['name'][$language_id] = $this->language->get('error_name');
      		}
    	}
		/*
    	if ((strlen(utf8_decode($this->request->post['model'])) < 1) || (strlen(utf8_decode($this->request->post['model'])) > 64)) {
      		$this->error['model'] = $this->language->get('error_model');
    	}
		*/
        /*
    	if ((strlen(utf8_decode($this->request->post['sku'])) < 1) || (strlen(utf8_decode($this->request->post['sku'])) > 64)) {
      		$this->error['sku'] = $this->language->get('error_sku');
    	}
		*/
        
        if (isset($this->request->post['product_color'])){
            $this->error['color'] = array();
            foreach($this->request->post['product_color'] as $key => $value){
                if(($value['name'] == '') || ($value['color_image'] == '') || ($value['color_image'] == 'no_image.jpg')){
                    $this->error['color'][$key] = $this->language->get('error_color');   
                }
            }
            if(empty($this->error['color'])){
                unset($this->error['color']);
            }
    	}
        
    	if (!$this->error) {
			return TRUE;
    	} else {
			if (!isset($this->error['warning'])) {
				$this->error['warning'] = $this->language->get('error_required_data');
			}
      		return FALSE;
    	}
  	}
	
  	private function validateDelete() {
    	if (!$this->user->hasPermission('modify', 'catalog/product')) {
      		$this->error['warning'] = $this->language->get('error_permission');  
    	}
		
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
  	}
  	
  	private function validateCopy() {
    	if (!$this->user->hasPermission('modify', 'catalog/product')) {
      		$this->error['warning'] = $this->language->get('error_permission');  
    	}
		
		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
  	}
	
	public function category() {
		$this->load->model('catalog/product');
		
		if (isset($this->request->get['category_id'])) {
			$category_id = $this->request->get['category_id'];
		} else {
			$category_id = 0;
		}
		
		$product_data = array();
		
		$results = $this->model_catalog_product->getProductsByCategoryId($category_id);
		
		foreach ($results as $result) {
			$product_data[] = array(
				'product_id' => $result['product_id'],
				'name'       => $result['name'],
				'model'      => $result['model'],
                'sku'        => $result['sku']
			);
		}
		
		$this->load->library('json');
		
		$this->response->setOutput(Json::encode($product_data));
	}
	
	public function related() {
		$this->load->model('catalog/product');
		
		if (isset($this->request->post['product_related'])) {
			$products = $this->request->post['product_related'];
		} else {
			$products = array();
		}
	
		$product_data = array();
		
		foreach ($products as $product_id) {
			$product_info = $this->model_catalog_product->getProduct($product_id);
			
			if ($product_info) {
				$product_data[] = array(
					'product_id' => $product_info['product_id'],
					'name'       => $product_info['name'],
					'model'      => $product_info['model']
				);
			}
		}
		
		$this->load->library('json');
		
		$this->response->setOutput(Json::encode($product_data));
	}
    
    public function colorValues(){
        $this->load->model('catalog/attribute');  
        $this->load->model('tool/image');
        
        if(isset($this->request->post['color_group_id'])){
            $color_group_id = $this->request->post['color_group_id'];
        } 
        else {
            $color_group_id = 0;
        }
        
        $color_values = array();        
        $results = $this->model_catalog_attribute->getAttributeValues($color_group_id);   
                  
        foreach($results as $result){
            if($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
                $image_file = $result['image'];  
                $preview = $this->model_tool_image->resizeNoScale($image_file, 100, 20);  
            } 
            else {
                $image_file = 'no_image.jpg';
                $preview = $this->model_tool_image->resize($image_file, 100, 20);  
            }
            
            $color_values[] = array(
                'attribute_id'  => $color_group_id,
                'value_id'      => $result['value_id'],
                'name'          => $result['name'],
                'sort_order'    => $result['sort_order'],
                'image'         => $image_file,
                'preview'       => $preview,
            );
        } 
        
        $this->load->library('json');                                   
        $this->response->setOutput(Json::encode($color_values));  
    }
    
    public function attributeValues(){
        $this->load->model('catalog/attribute');  
        
        if(isset($this->request->post['attribute_id'])){
            $attribute_id = $this->request->post['attribute_id'];
        } 
        else {
            $attribute_id = 0;
        }
        
        $attribute_values = array();        
        $results = $this->model_catalog_attribute->getAttributeValues($attribute_id);                     
        foreach($results as $result){            
            $attribute_values[] = array(
                'attribute_id'  => $attribute_id,
                'value_id'      => $result['value_id'],
                'name'          => $result['name'],
                'sort_order'    => $result['sort_order'],
            );
        } 
        
        $this->load->library('json');                                   
        $this->response->setOutput(Json::encode($attribute_values));  
    }
    
    private function getFilter(){
        $url = ''; 
        
        if (isset($this->request->get['filter_name'])) {
            $url .= '&filter_name=' . $this->request->get['filter_name'];
        }
        
        if (isset($this->request->get['filter_model'])) {
            $url .= '&filter_model=' . $this->request->get['filter_model'];
        }
            
        if (isset($this->request->get['filter_quantity'])) {
            $url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
        }

        if (isset($this->request->get['filter_category'])) {
            $url .= '&filter_category=' . $this->request->get['filter_category'];
        }

        if (isset($this->request->get['filter_sku'])) {
            $url .= '&filter_sku=' . $this->request->get['filter_sku'];
        }
            
        if (isset($this->request->get['filter_manufacturer'])) {
            $url .= '&filter_manufacturer=' . $this->request->get['filter_manufacturer'];
        }
            
        if (isset($this->request->get['filter_status'])) {
            $url .= '&filter_status=' . $this->request->get['filter_status'];
        } 
        
        return $url;   
    }
    
    private function getUrlParams(){
        $url = $this->getFilter();
        
        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }
        
        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }
        
        return $url;
    }
	
	public function autocomplete() {
		$json = array();
		
		if (isset($this->request->get['filter_name_sku']) || isset($this->request->get['filter_category'])) {
			$this->load->model('catalog/product');
			
			if (isset($this->request->get['filter_name_sku'])) {
				$filter_name_sku = $this->request->get['filter_name_sku'];
			}
			else {
				$filter_name_sku = null;
			}
					
			if (isset($this->request->get['filter_category'])) {
				$filter_category = $this->request->get['filter_category'];
			}
			else {
				$filter_category = null;
			}
			
			if (isset($this->request->get['filter_manufacturer'])) {
				$filter_manufacturer = $this->request->get['filter_manufacturer'];
			}
			else {
				$filter_manufacturer = null;
			}
			
			if (isset($this->request->get['limit'])) {
				$limit = $this->request->get['limit'];	
			}
			else {
				$limit = 20;	
			}			
						
			$data = array(
				'filter_name_sku'      	=> $filter_name_sku,				
				'filter_category'  		=> $filter_category,
				'filter_manufacturer' 	=> $filter_manufacturer,
				'start'               	=> 0,
				'limit'               	=> $limit
			);
			
			$results = $this->model_catalog_product->getProductsAutocomplete($data);			
			foreach ($results as $result) { 
				$option_data = array();				
				$product_options = $this->model_catalog_product->getActiveProductOptions($result['product_id']);	
				
				foreach ($product_options as $product_option) {		
					$option_value_data = array();						
					foreach ($product_option['option_value'] as $product_option_value) {
						$option_value_data[] = array(
							'product_option_value_id' => $product_option_value['product_option_value_id'],								
							'name'                    => $product_option_value['name'],
							'price'                   => $product_option_value['price'],
							'price_format'            => $product_option_value['price'] ? $this->currency->formatPrice($product_option_value['price'], $this->config->get('config_currency')) : 0,
							'prefix'            	  => $product_option_value['prefix']
						);	
					}
					
					$option_data[] = array(
						'product_option_id' => $product_option['product_option_id'],							
						'name'              => $product_option['name'],						
						'option_value'      => $option_value_data,							
					);						
				}				
				
				$json[] = array(
					'product_id'	=> $result['product_id'],
					'name'       	=> html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'),	
					'price'      	=> $result['price'],
					'price_format'  => $this->currency->formatPrice($result['price'], $this->config->get('config_currency')),
					'option'     	=> $option_data,					
					'sku'      	 	=> $result['sku'],
				);	
			}
		}

		$this->response->setOutput(json_encode($json));
	}
}
?>
